import Http from '@/common/js/http.js'
// state //==> this.$store.state.index
const state = () => ({
	cartNumber:0,						//购物车数量
	wholeSaleNumber:0,					//进货单数量
	themeStyle:'',
	Development: 1,
	addonIsExit: {
		bundling: 0,
		coupon: 0,
		discount: 0,
		fenxiao: 0,
		gift: 0,
		groupbuy: 0,
		manjian: 0,
		memberconsume: 0,
		memberrecharge: 0,
		memberregister: 0,
		membersignin: 0,
		memberwithdraw: 0,
		pintuan: 0,
		pointexchange: 0,
		seckill: 0,
		store: 0,
		topic: 0
	},	
})

// mutations
const mutations = {
	setCartNumber(state, cartNumber) {
		state.cartNumber = cartNumber
	},
	setWholeSaleNumber(state, wholeSaleNumber) {
		state.wholeSaleNumber = wholeSaleNumber
	},
	setThemeStyle(state, ThemeStyle) {
		state.themeStyle = ThemeStyle
	},
	setAddonIsexit(state,addonIsExit){
		state.addonIsExit=Object.assign(state.addonIsExit, addonIsExit);
	}
}

// actions
const actions = {
	//查询购物车数量
	getCartNumber(context) {
		if (uni.getStorageSync("token")) {
			return new Promise((resolve, reject)=>{
				Http.sendRequest({
					url: '/api/cart/count',
					success: res => {
						// console.log(res,'购物车数量');
						if (res.code == 0) {
							context.commit('setCartNumber', res.data)
							resolve(res.data)
						}
					}
				});
			})
		}
	},
	//查询进货单数量
	getWholeSaleNumber(context){
		Http.sendRequest({
			url: '/wholesale/api/cart/count',
			success: res => {
				if (res.code == 0) {
					context.commit('setWholeSaleNumber',res.data)
				}
			}
		});
	},
	getThemeStyle(context) {
		if(uni.getStorageSync('setThemeStyle')){
			context.commit('setThemeStyle', uni.getStorageSync('setThemeStyle'))
		}
		Http.sendRequest({
			url: '/api/diyview/style',
			success: res => {
				if (res.code == 0) {
					context.commit('setThemeStyle', res.data.style_theme)
					uni.setStorageSync('setThemeStyle',res.data.style_theme)
				}
			}
		});
	},
	// 获取插件是否安装
	getAddonIsexit(context) {
		if(uni.getStorageSync('memberAddonIsExit')){
			context.commit('setAddonIsexit', uni.getStorageSync('memberAddonIsExit'))
		}
		Http.sendRequest({
			url: '/api/addon/addonisexit',
			success: res => {
				if (res.code == 0) {
					uni.setStorageSync('memberAddonIsExit',res.data);
					context.commit('setAddonIsexit', res.data)
				}
			}
		})
	},
}

// getters
const getters = {

}

export default {
	namespaced: true,
	state,
	getters,
	actions,
	mutations
}
